package com.yoomiito.app.widget.dialog;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.Tools;

public class TipDialog extends BaseDialog {

    private String content;
    private String[] spanTips;
    private TextView contentTv;
    private TextView mTitleTv;
    private TextView mCancelTv;
    private String title;
    private String cancelText = "";
    private String sureText = "";
    private int cancelColor = -1;
    private View divLine;
    public TextView sureTv;
    private View.OnClickListener mClickListener;
    private View.OnClickListener mCancelClickListener;
    private OnSpanTipClickListenr mOnSpanTipClickListener;
    private boolean canBack = true;
    private boolean mCanceledOnTouchOutside = true;
    private boolean mTitleTvVisible;
    private int height = 0;

    public TipDialog(@NonNull Context context) {
        this(context, 0);
    }

    public TipDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    protected TipDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        mTitleTv = view.findViewById(R.id.dialog_tip_title);
        mTitleTv.setText(title);
        mTitleTv.setVisibility(mTitleTvVisible ? View.VISIBLE : View.GONE);
        contentTv = view.findViewById(R.id.dialog_tip_content);
        divLine = view.findViewById(R.id.item_line);
        sureTv = view.findViewById(R.id.item_sure);
        sureTv.setText(sureText);
        divLine.setVisibility(TextUtils.isEmpty(sureText) || TextUtils.isEmpty(cancelText) ? View.GONE : View.VISIBLE);
        sureTv.setVisibility(TextUtils.isEmpty(sureText) ? View.GONE : View.VISIBLE);
        contentTv.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (spanTips != null && spanTips.length > 0) {
            setSpanTipClick(content, spanTips);
        } else {
            contentTv.setText(content);
        }
        mCancelTv = view.findViewById(R.id.item_cancel);
        mCancelTv.setVisibility(TextUtils.isEmpty(cancelText) ? View.GONE : View.VISIBLE);
        mCancelTv.setText(cancelText);
        if (cancelColor != -1) {
            mCancelTv.setTextColor(cancelColor);
        }
        if (mCancelClickListener == null) {
            mCancelTv.setOnClickListener(v ->
                    dismiss()
            );
        } else {
            mCancelTv.setOnClickListener(mCancelClickListener);
        }
        if (mClickListener != null)
            sureTv.setOnClickListener(mClickListener);
        setCanceledOnTouchOutside(mCanceledOnTouchOutside);

        return view;
    }


    public void setHeightPercent(int height) {
        if (contentTv != null) {
            contentTv.setHeight(Tools.dip2px(mContext, height));
        }

    }

    ;

    @Override
    public void onBackPressed() {
        if (canBack) super.onBackPressed();
    }

    public TipDialog setContent(String content) {
        if (contentTv != null) {
            contentTv.setText(content);
        }
        this.content = content;
        return this;
    }

    /**
     * 文案中有需要高亮、点击的部分
     *
     * @param fullTips
     * @param spanTips 需要点击的文案
     * @return
     */
    public TipDialog setContentClick(String fullTips, String... spanTips) {
        if (contentTv != null) {
            setSpanTipClick(fullTips, spanTips);
        }
        this.content = fullTips;
        this.spanTips = spanTips;
        return this;
    }

    public TipDialog setTitle(String title) {
        if (mTitleTv != null) {
            mTitleTv.setText(title);
        }
        this.title = title;
        return this;
    }

    public TipDialog setTitleVisible(boolean show) {
        if (mTitleTv != null) {
            mTitleTv.setVisibility(show ? View.VISIBLE : View.GONE);
        }
        this.mTitleTvVisible = show;
        return this;
    }

    public TipDialog setCancelText(String cancel) {
        if (mCancelTv != null) mCancelTv.setText(cancel);
        this.cancelText = cancel;
        return this;
    }

    public TipDialog setCancelTextColor(int color) {
        if (mCancelTv != null) mCancelTv.setTextColor(color);
        this.cancelColor = color;
        return this;
    }

    public TipDialog setSureText(String sureText) {
        this.sureText = sureText;
        if (sureTv != null) {
            sureTv.setText(sureText);
        }
        return this;
    }

    public TipDialog setClickListener(View.OnClickListener clickListener) {
        mClickListener = clickListener;
        return this;
    }

    public TipDialog setmCanceledOnTouchOutside(boolean mCanceledOnTouchOutside) {
        this.mCanceledOnTouchOutside = mCanceledOnTouchOutside;
        return this;
    }

    /**
     * 取消按钮默认点击后取消弹窗。如果点击取消按钮后有其他操作，请设置监听事件。
     *
     * @param mCancelClickListener
     * @return
     */
    public TipDialog setmCancelClickListener(View.OnClickListener mCancelClickListener) {
        this.mCancelClickListener = mCancelClickListener;
        return this;
    }

    /**
     * 返回键是否可以使用  false不可以使用 true 可以使用
     *
     * @param canBack
     * @return
     */
    public TipDialog setCanBack(boolean canBack) {
        this.canBack = canBack;
        return this;
    }

    /**
     * @param fullTips 整个文案
     * @param spanTips 需要点击的文案
     */
    private void setSpanTipClick(String fullTips, String[] spanTips) {
        SpannableString resultTipStr = new SpannableString(fullTips);
        for (String spanTip : spanTips) {
            int start = fullTips.indexOf(spanTip);
            int end = start + spanTip.length();
            if (start >= 0) {
                // 可点击
                resultTipStr.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@androidx.annotation.NonNull View widget) {
                        if (mOnSpanTipClickListener != null)
                            mOnSpanTipClickListener.onClick(spanTip);
                    }

                    @Override
                    public void updateDrawState(@androidx.annotation.NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(ResUtil.getColor(R.color.color_FF035B));
                    }
                }, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }

        contentTv.setMovementMethod(LinkMovementMethod.getInstance());
        contentTv.setText(resultTipStr);
    }

    public void setmOnSpanTipClickListener(OnSpanTipClickListenr mOnSpanTipClickListener) {
        this.mOnSpanTipClickListener = mOnSpanTipClickListener;
    }

    public interface OnSpanTipClickListenr {
        void onClick(String spanTip);
    }
}
